#ifndef LINEITEM_H
#define LINEITEM_H

#include <QButtonGroup>
#include <QAction>
#include <QActionGroup>
#include <QToolBar>
#include <QToolButton>
#include <QGridLayout>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QGroupBox>
#include <QListWidget>
#include <QGraphicsItem>
#include <QGraphicsLineItem>
#include <QGraphicsRectItem>
#include <QGraphicsSceneMouseEvent>
#include <QLineF>
#include <QRectF>
#include "GraphicsScene.h"
#include "ControlPoint.h"

//checkit
#ifdef Q_WS_WIN
#define MY_EXPORT __declspec(dllexport)
#else
#define MY_EXPORT
#endif

class LineItem;
///*
class MY_EXPORT ItemControlPoint : public QGraphicsRectItem {
 public:
  //  QRectF rect; //no need??
  static LineItem *cast( QGraphicsItem* );
  static const QString CLASSNAME;
  QString className;
  QBrush defaultBrush;
  QPen defaultPen;
  enum { Type = UserType + 101 };
  int type() const { return Type; }

  ItemControlPoint(){}
  ItemControlPoint( QGraphicsItem * parent = 0 ); 
  ~ItemControlPoint(){}
  QGraphicsItem* owner;

  virtual QRectF boundingRect() const;
  virtual void paint( QPainter* ppainter, const QStyleOptionGraphicsItem*, QWidget* ); 
  virtual void mousePressEvent( QGraphicsSceneMouseEvent *pe );
  virtual void mouseMoveEvent( QGraphicsSceneMouseEvent *pe );
  virtual void mouseReleaseEvent( QGraphicsSceneMouseEvent *pe );

 private:
  bool selected;

 };
//*/
class MY_EXPORT LineItem : public QGraphicsLineItem {
  //  Q_OBJECT
 public:
  //QLineF line; //??? require or no???
  static LineItem *cast( QGraphicsItem* );
  static const QString CLASSNAME;
  QString className;
  QBrush defaultBrush;
  QPen defaultPen;
  enum { Type = UserType + 100  };
  int type() const { return Type; }

  LineItem(){} //write constructor copy!!!!
  LineItem( QGraphicsItem * parent = 0 );
  LineItem( const QLineF &_line, QGraphicsItem * parent=0, Tinkercell::GraphicsScene * scene=0);
  ~LineItem(){}
  //  void showControlPoint();
  //  void hideControlPoint();

  //  void visibleControlPoint( bool set_visible );
  //  bool isVisibleControlPoint();



  /*!brief add begin, end and midle control points to line */ 
  //  void addControlPoint( );

  virtual QRectF boundingRect() const;
  virtual void paint( QPainter* ppainter, const QStyleOptionGraphicsItem*, QWidget* );
  virtual void mousePressEvent( QGraphicsSceneMouseEvent *pe );

  QList<QGraphicsItem*> list_control_point;

 private:
  struct common {
    int y0; //automate 0
    int y1; //automate 1
  };
  common cm;
  enum {
    s0, //stop
    s1, //start
    s2  //created
  };
typedef  enum {
    e0, //creat item
    e1, //selected item
    e2  //move control point from item
  }Event;
 void run( Event e );
 void step( Event e );

 class MY_EXPORT ControlPoint : public Tinkercell::ControlPoint {
 public:
     LineItem * powner_line_item;

    /*! \brief for enabling dynamic_cast */

    enum { Type = UserType + 101 };
    virtual int type() const { return Type; }
    ~ControlPoint();
    //    ControlPoint(){}
  //    ControlPoint( const ControlPoint & copy );
  //    ControlPoint( LineItem * reaction_ptr = 0, QGraphicsItem * parent = 0 );
    //    ControlPoint( const QPointF & pos, LineItem * reaction_ptr = 0, QGraphicsItem * parent = 0 );
    //    virtual Tinkercell::ControlPoint* clone() const;
    //    virtual ItemHandle * handle() const;
    //    virtual void setHandle( ItemHandle * );
 };


  QList<Tinkercell::ControlPoint*> control_points;
  //  QList<QGraphicsRectItem*> list_control_point;
  //  QList<QGraphicsItem*> list_control_point;
  //no QList<ItemControlPoint*> list_control_point;
  bool selected;
  Tinkercell::GraphicsScene * scene;
};



#endif //LINEITEM_H
